import lexer
import ll1
import parse

# def main():
#     snl_program = '''
# program example
# var integer x, y, z;
# begin
#     x := 10;
#     y := 20;
#     z := x + y;
# end.
#     '''
#
#     tokens = lexer.lexer(snl_program)
#     for t in tokens:
#         print(t.value, t.token_type)
#
#     try:
#         parse.parse_program(tokens)
#         if tokens:
#             print("Syntax error: Extra tokens found")
#         else:
#             print("Syntax is correct")
#     except Exception as e:
#         print(e)


# 测试递归下降

# if __name__ == "__main__":
#     input_code = '''
# program example
# var integer x, y, z;
# char m;
# begin
#     x := 10;
#     y := 20;
#     z := x + y;
# end.
#     '''
#     tokens = lexer.lexer(input_code)
#     tree = parse.parse_program(tokens)
#     parse.print_tree(tree)

cur = """
    program example
    var integer a, b, c;
    begin
        a := b + c * 2 - 4;
        b := (a + 1) * 3;
    end.
"""

cur1="""
program p 
type tl = integer; 
type tk= char; 
var integer v1 ,v2; 
procedure q(integer i); 
var integer a; 
begin
a:=i;
write(a);
end
begin
read(v1);
if v1<10
then v1:=v1+10 
else v1 :=v1-10 
fi;
q(vi)
end.
"""

cur2="""
program example
    var x, y, z;
    begin
        x := 1;
        y := 2;
        z := x + y * 3;
    end.
"""
if __name__ == "__main__":
    input_code = """
   program p 
   type t = integer; 
   var t v1;
    char v2; 
    begin
    read(v1); 
    v1:=v1*10; 
    v2:= 'a'; 
    write(v1);
    end.
    """


    tokens = lexer.lexer(cur2)
    print(tokens)

    # for t in tokens:
    #     print(t.token_type+" : ",t.value)

    # print("--------------- 递归下降 ----------------")
    # tree = parse.parse_program(tokens)
    # for node in tree:
    #     print(node)
    # print(tree)
    # parse.print_tree(tree)
    print("--------------- LL1 ----------------")

    tree = ll1.parse(tokens,  ll1.ll1_table)
    ll1.print_tree(tree)


